﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.GIS;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using System.Net;
using Jumpton.SFA.Server.BusinessRule.MasterData;

public partial class BusinessData_CustomerMapList : PageBase, ICallbackEventHandler
{

    public string _content;
    public int zoomLevel = 1;     //地图放大级别  1.省级，2.市级，3.区级
    protected void Page_Load(object sender, EventArgs e)
    {

        this.CustomerList.Items.Clear();
        if (!IsPostBack)
        {

            BindRegion();
        }
    }

    protected void RegionOnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (sender == ddlCountry)
        {
            ddlState.DataSource = new RegionManager().GetRegionDistinctData(MyStateManager.DomainID, 1, ddlCountry.SelectedValue);
            ddlState.DataBind();
            ddlState.Items.Insert(0, new ListItem("全部", ""));
            RegionOnSelectedIndexChanged(ddlState, e);
        }
        else if (sender == ddlState)
        {
            ddlCity.DataSource = new RegionManager().GetRegionDistinctData(MyStateManager.DomainID, 2, ddlState.SelectedValue);
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, new ListItem("全部", ""));
            RegionOnSelectedIndexChanged(ddlCity, e);
        }
        else if (sender == ddlCity)
        {
            ddlDistrict.DataSource = new RegionManager().GetRegionDistinctData(MyStateManager.DomainID, 3, ddlCity.SelectedValue);
            ddlDistrict.DataBind();
            ddlDistrict.Items.Insert(0, new ListItem("全部", ""));
        }

    }

    private void BindRegion()
    {

        var countries = new RegionManager().GetRegionDistinctData(MyStateManager.DomainID, 0, null);
        ddlCountry.DataSource = countries;
        ddlCountry.DataBind();
        if (countries != null && countries.Rows.Count > 0)
        {
            ddlState.DataSource = new RegionManager().GetRegionDistinctData(MyStateManager.DomainID, 1, countries.Rows[0]["COUNTRY_CODE"].ToString());
            ddlState.DataBind();
        }
        ddlState.Items.Insert(0, new ListItem("全部", ""));
        ddlCity.Items.Clear();
        ddlCity.Items.Insert(0, new ListItem("全部", ""));
        ddlDistrict.Items.Clear();
        ddlDistrict.Items.Insert(0, new ListItem("全部", ""));


    }



    public void RaiseCallbackEvent(string the_string)
    {
        _content = the_string;
    }

    public string GetCallbackResult()
    {
        //  拆分由客户端传过来的参数
        string[] parts = _content.Split('|');

        //  生成ServerMethod的参数队列
        object[] theArgs = new object[parts.Length - 1];
        for (int int_index = 1; int_index < parts.Length; int_index++)
            theArgs[int_index - 1] = parts[int_index];

        //  调用ServerMethod
        return (string)GetType().GetMethod(parts[0]).Invoke(this, theArgs);
    }





    public void LoadMaps()
    {
        //必须选择到省级
        if ("" == ddlState.SelectedValue.Trim())
        {
            ShowMessage("至少选择到 省/直辖市 级");
            return;
        }

        //查询条件拼接
        StringBuilder sbWhere = new StringBuilder();
        if ("" != ddlCountry.SelectedValue.Trim())
        {
            sbWhere.AppendFormat(" and ci.COUNTRY_CODE='{0}' ", ddlCountry.SelectedValue);
        }
        if ("" != ddlState.SelectedValue.Trim())
        {
            sbWhere.AppendFormat(" and ci.STATE='{0}' ", ddlState.SelectedValue);
            zoomLevel = 1;
        }
        if ("" != ddlCity.SelectedValue.Trim())
        {
            sbWhere.AppendFormat(" and ci.CITY='{0}' ", ddlCity.SelectedValue);
            zoomLevel = 2;
        }
        if ("" != ddlDistrict.SelectedValue.Trim())
        {
            sbWhere.AppendFormat(" and ci.DISTRICT='{0}' ", ddlDistrict.SelectedValue);
            zoomLevel = 3;
        }
        DataTable dt = new CustomerManager().GetCustomerInfoByWhere(MyStateManager.DomainID, sbWhere.ToString());
        if (dt.Rows.Count > 0)
        {
            this.CustomerList.Items.Clear();
            ListItem li = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int no = i + 1;
                String QD = "";
                if (!String.IsNullOrEmpty(dt.Rows[i]["LOCAL_LEVEL1_CODE"].ToString()))
                {
                    try
                    {
                        DictionaryItems item = new DictionaryItemsManager().Load(Decimal.Parse(dt.Rows[i]["LOCAL_LEVEL1_CODE"].ToString()));

                        if (item != null)
                        {
                            QD = item.Name;
                        }
                    }
                    catch
                    {

                    }

                }
                String itemStr = dt.Rows[i]["NAME"]    //门店名称
                            + "," + dt.Rows[i]["CODE"]        //客户代码
                            + "," + dt.Rows[i]["ADDRESS_LINE"]        //地址
                            + "," + dt.Rows[i]["SLOG_CODE"]   //联系人
                            + "," + dt.Rows[i]["IDENTITY1"]   //经度
                            + "," + dt.Rows[i]["IDENTITY2"]   //纬度
                            + "," + dt.Rows[i]["TELEPHONE"]   //联系电话
                            + "," + QD //门店渠道
                            + "," + no                              //标注数字
                            + "," + dt.Rows[i]["GEO_LEVEL_CODE"] //商圈分级
                            + "," + dt.Rows[i]["ID"] //客户ID
                            ;
                li = new ListItem(no + ":" + dt.Rows[i]["NAME"].ToString(), itemStr);



                this.CustomerList.Items.Add(li);
            }

            this.CustomerList.Attributes.Add("onchange", "ShowIW(this);return false;");
        }

    }

    protected void btnSrh_Click(object sender, EventArgs e)
    {
        LoadMaps();
    }

}
